<?php
namespace Utuo\WechatAdvance;

class Address {
	
	/**
	 * 获得地址签名
	 * @param array $data
	 * array (
	 * 	 "appId" => $data['appId'],
	 * 	 "accesstoken" => $data['accesstoken'],
	 * 	 "nonceStr" => $data['nonceStr'],
	 * 	 "timeStamp" => $data['timeStamp'],
	 * 	 "url" => $data['url'],
	 * )
	 * @return string
	 */
	public function getSign( $data ) {
		$jsApiObj = array(
			"appId" => $data['appId'],
			"accesstoken" => $data['accesstoken'],
			"nonceStr" => $data['nonceStr'],
			"timeStamp" => $data['timeStamp'],
			"url" => $data['url'],
		);
		$addresssign = $this->createSign($jsApiObj);
		return $addresssign;
	}
	
	/**
	 * 签名
	 * @param type $params
	 */
	private function createSign( $params, $keys ) {
		ksort($params);
		reset($params);
		 
		$arg = '';
		foreach ($params as $key => $value) {
			empty($value) or $arg[] = "{$key}={$value}";
		}
		return sha1(join('&', $arg));
	}
	
}